/* @formatter:off
 * Copyright (c) 2011 Lucas <lucas03093@gmail.com>
 * 
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* @formatter:on */

package nu.korkad.daytracker.alertdialog;

import nu.korkad.daytracker.R;
import nu.korkad.daytracker.info.AppInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableString;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class Changelog
{
    /**
     * Creates a dialog (popup).
     * 
     * @param context
     * @param buttonText
     *            String
     * @param changelog
     *            String
     * @param newVersion
     *            String
     * @return AlertDialog
     */
    public static AlertDialog create(final Context context, String posButtonText, String negButtonText, String changelog,
            String newVersion, final String updateURL)
    {
        // Get layout
        View layout = LayoutInflater.from(context).inflate(R.layout.activity_settings_alertdialog_update, null);
        
        String oldVersion;
        
        // Get version info
        oldVersion = AppInfo.getVersionName(context);
        
        // Strings
        String title = context.getString(R.string.app_name) + " " + context.getString(R.string.dialog_update_text_title);
        String versionInfo = context.getString(R.string.dialog_update_text_oldversion) + " " + oldVersion + "\n"
                + context.getString(R.string.dialog_update_text_newversion) + " " + newVersion;
        
        // Set text
        final TextView message = (TextView) layout.findViewById(R.id.layout_settings_alertdialog_update_textview);
        final SpannableString s = new SpannableString(context.getString(R.string.dialog_update_text_changelog) + "\n" + changelog);
        
        // Set some padding
        message.setPadding(5, 5, 5, 5);
        
        // Set the dialog text
        message.setText(versionInfo + "\n\n" + s);
        
        // Turn web addresses to links
        Linkify.addLinks(message, Linkify.ALL);
        
        return new AlertDialog.Builder(context).setTitle(title).setCancelable(true).setPositiveButton(posButtonText, new OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(updateURL)));
            }
        }).setNegativeButton(negButtonText, null).setView(layout).create();
    }
}
